package org.springblade.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.system.entity.Doctor;
import org.springblade.modules.system.mapper.DoctorMapper;
import org.springblade.modules.system.service.DoctorService;
import org.springblade.modules.system.utils.BusinessException;
import org.springblade.modules.system.utils.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {

	@Autowired
	private DoctorMapper doctorMapper;

	public PageBounds<Doctor> openDoctorTable(String doctorName, String createTimeMax, String createTimeMin, int currentPage, int pageSize) throws Exception {
		try {
			final int totalSize = doctorMapper.countByExample(doctorName, createTimeMax, createTimeMin);
			PageBounds<Doctor> pageBounds = new PageBounds<Doctor>(currentPage, totalSize, pageSize);
			List<Doctor> list = doctorMapper.selectByLimitPage(doctorName, createTimeMax, createTimeMin, pageBounds.getOffset(), pageBounds.getPageSize());
			pageBounds.setPageList(list);
			return pageBounds;
		} catch (Exception e) {
			throw new BusinessException("获取医生列表失败！", e.getMessage());
		}
	}

	public List<Doctor> selectAllDoctor() throws BusinessException {
		try {
			return doctorMapper.selectAllDoctor();
		} catch (Exception e) {
			throw new BusinessException("查询失败！", e.getMessage());

		}
	}

	public void deleteDoctorByIds(List<Integer> ids) throws BusinessException {
		try {
			doctorMapper.deleteByPrimaryKey(ids);
		} catch (Exception e) {
			throw new BusinessException("删除失败！", e.getMessage());
		}
	}

	public int deleteDoctorById(Integer id) {

		return doctorMapper.deleteDoctorById(id);
	}

	public int addDoctor(Doctor doctor) throws BusinessException {
		try {
			return doctorMapper.addDoctor(doctor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	public int updateDoctor(Doctor doctor) {
		return doctorMapper.updateDoctor(doctor);
	}

	public Doctor findById(Integer id) {
		return doctorMapper.findById(id);
	}
}
